﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using BUS;
using DTO;
using DevComponents.DotNetBar;
using System.Collections;


namespace Nhom16_PTTKHTTT_12HCB
{
    public partial class frmPhucVu : DevComponents.DotNetBar.Office2007Form
    {
        DataTable dt = new DataTable();
        DataTable td = new DataTable();
        BUS.GiamGia_BUS ggb;
        BUS.DiemCong_BUS dcb;
        BUS.Ve_HoaDon_BUS vehoadon_bus;
        BUS.Ban_BUS ban_bus;
        BUS.ThucDonDichVuKhach_BUS thucdon;
        DTO.ThucDonDichVuKhach thucdondichvu_dto;

        bool flagThanToan = true;

        //BienToanCuc bien = new BienToanCuc();

        public frmPhucVu()
        {
            InitializeComponent();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPhucVu_Load(object sender, EventArgs e)
        {
            //cboBuoi.Items.Add("chọn");
            cboBuoi.Items.Add("Trưa");
            cboBuoi.Items.Add("Tối");
            cboNgay.Items.Add("Thường");
            cboNgay.Items.Add("Đặc biệt");

            // load dữ liệu danh sách tỷ lệ giảm giá % lên combobox;
            ggb = new BUS.GiamGia_BUS();
            dt = ggb.getDanhSachTiLeGiam();
            cboGiamGia.DisplayMember = "TiLeGiam";
            cboGiamGia.ValueMember = "TiLeGiam";
            cboGiamGia.DataSource = dt;

            KiemTraTinhTrangBan1();
            KiemTraTinhTrangBan2();
            KiemTraTinhTrangBan3();
            KiemTraTinhTrangBan4();
            KiemTraTinhTrangBan5();
            KiemTraTinhTrangBan6();
            KiemTraTinhTrangBan7();
            KiemTraTinhTrangBan8();
            KiemTraTinhTrangBan9();
            KiemTraTinhTrangBan10();

            thucdon = new ThucDonDichVuKhach_BUS();
            td = thucdon.getDanhSachDichVuKhac();
            dgvDanhSachThucDon.DataSource = td;
        }

        private void KiemTraTinhTrangBan1()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan1.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan1.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan1.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan1.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan2()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan2.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan2.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan2.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan2.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan3()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan3.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan3.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan3.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan3.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan4()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan4.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan4.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan4.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan4.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan5()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan5.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan5.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan5.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan5.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan6()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan6.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan6.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan6.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan6.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan7()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan7.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan7.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan7.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan7.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan8()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan8.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan8.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan8.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan8.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan9()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan9.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan9.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan9.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan9.ColorTable = eButtonColor.BlueOrb;
            }
        }

        private void KiemTraTinhTrangBan10()
        {
            ban_bus = new Ban_BUS();
            DataTable ban = new DataTable();
            ban = ban_bus.getKiemTraTinhTrangBan(btnBan10.Text.Trim());
            string tinhtrang = ban.Rows[0]["TinhTrang"].ToString();
            if (tinhtrang == "0")
            {
                btnBan10.ColorTable = eButtonColor.MagentaWithBackground;
            }
            else if (tinhtrang == "1")
            {
                btnBan10.ColorTable = eButtonColor.Magenta;
            }
            else if (tinhtrang == "2")
            {
                btnBan10.ColorTable = eButtonColor.BlueOrb;
            }
        }


        private void cboBuoi_TextChanged(object sender, EventArgs e)
        {
            if (cboBuoi.Text == "" || cboNgay.Text == "")
            {
                txtSoVeNguoiLon.ReadOnly = true;
            }
            else
                txtSoVeNguoiLon.ReadOnly = false;

            if (cboBuoi.Text == "" || cboNgay.Text == "")
            {
                txtSoVeTreEm.ReadOnly = true;
            }
            else
                txtSoVeTreEm.ReadOnly = false;

            if (cboBuoi.Text == "Tối")
            {
                if (cboNgay.Text == "Thường")
                {
                    lbGiaVeNguoiLon.Text = "200000";
                    lbTreEm.Text = "100000";
                    lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text)*(float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                    lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text)*(float.Parse(lbTreEm.Text))).ToString();
                    lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                }
                else
                    if (cboNgay.Text == "Đặc biệt")
                    {
                        lbGiaVeNguoiLon.Text = "250000";
                        lbTreEm.Text = "150000";
                        lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text) * (float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                        lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text) * (float.Parse(lbTreEm.Text))).ToString();
                        lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                    }
            }
            else
            {
                if (cboNgay.Text == "Thường")
                {
                    lbGiaVeNguoiLon.Text = "200000";
                    lbTreEm.Text = "100000";
                    lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text) * (float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                    lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text) * (float.Parse(lbTreEm.Text))).ToString();
                    lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                }
                else
                    if (cboNgay.Text == "Đặc biệt")
                    {
                        lbGiaVeNguoiLon.Text = "230000";
                        lbTreEm.Text = "100000";
                        lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text) * (float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                        lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text) * (float.Parse(lbTreEm.Text))).ToString();
                        lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                    }
            }

            //}
        }

        private void cboNgay_TextChanged(object sender, EventArgs e)
        {
            if (cboBuoi.Text == "" || cboNgay.Text == "")
            {
                txtSoVeNguoiLon.ReadOnly = true;
            }
            else
                txtSoVeNguoiLon.ReadOnly = false;

            if (cboBuoi.Text == "" || cboNgay.Text == "")
            {
                txtSoVeTreEm.ReadOnly = true;
            }
            else
                txtSoVeTreEm.ReadOnly = false;

            if (cboBuoi.Text == "Tối")
            {
                if (cboNgay.Text == "Thường")
                {
                    lbGiaVeNguoiLon.Text = "200000";
                    lbTreEm.Text = "100000";
                    lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text) * (float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                    lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text) * (float.Parse(lbTreEm.Text))).ToString();
                    lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                }
                else
                    if (cboNgay.Text == "Đặc biệt")
                    {
                        lbGiaVeNguoiLon.Text = "250000";
                        lbTreEm.Text = "150000";
                        lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text) * (float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                        lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text) * (float.Parse(lbTreEm.Text))).ToString();
                        lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                    }
            }
            else
            {
                if (cboNgay.Text == "Thường")
                {
                    lbGiaVeNguoiLon.Text = "200000";
                    lbTreEm.Text = "100000";
                    lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text) * (float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                    lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text) * (float.Parse(lbTreEm.Text))).ToString();
                    lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                }
                else
                    if (cboNgay.Text == "Đặc biệt")
                    {
                        lbGiaVeNguoiLon.Text = "230000";
                        lbTreEm.Text = "100000";
                        lbTongVeNguoiLon.Text = (float.Parse(txtSoVeNguoiLon.Text) * (float.Parse(lbGiaVeNguoiLon.Text))).ToString();
                        lbTongVeTreEm.Text = (float.Parse(txtSoVeTreEm.Text) * (float.Parse(lbTreEm.Text))).ToString();
                        lbTongTien.Text = (float.Parse(lbTongVeNguoiLon.Text) + (float.Parse(lbTongVeTreEm.Text))).ToString();
                    }
            }
        }

        private void txtSoVeNguoiLon_TextChanged(object sender, EventArgs e)
        {
            if (txtSoVeNguoiLon.Text == "")
            {
                txtSoVeNguoiLon.Text = 0.ToString();
            }
            float sove = float.Parse(txtSoVeNguoiLon.Text);
            float giatien = float.Parse(lbGiaVeNguoiLon.Text);
            float tongtien = sove * giatien;
            lbTongVeNguoiLon.Text = tongtien.ToString();
            lbTongTien.Text = (tongtien + float.Parse(lbTongVeTreEm.Text)).ToString();
        }

        private void txtSoVeTreEm_TextChanged(object sender, EventArgs e)
        {
            if (txtSoVeTreEm.Text == "")
            {
                txtSoVeTreEm.Text = 0.ToString();
            }
            float sove = float.Parse(txtSoVeTreEm.Text);
            float giatien = float.Parse(lbTreEm.Text);
            float tongtien = sove * giatien;
            lbTongVeTreEm.Text = tongtien.ToString();
            lbTongTien.Text = (tongtien + float.Parse(lbTongVeNguoiLon.Text)).ToString();
        }

        private void lbTongTien_TextChanged(object sender, EventArgs e)
        {
            lbPhaiTra.Text = lbTongTien.Text;
            if (float.Parse(lbTongTien.Text) >= 500000)
            {

                linkLabel1.Enabled = true;
            }
        }

        private void txtKHDua_TextChanged(object sender, EventArgs e)
        {
            if (txtKHDua.Text == "")
            {
                txtKHDua.Text = 0.ToString();
                btnThanhToan.Enabled = false;
            }
            float tienKHDua = float.Parse(txtKHDua.Text.Trim());
            float tienphaitra = float.Parse(lbPhaiTra.Text);
            float tienthoilai = tienKHDua - tienphaitra;
            lbThoiLai.Text = tienthoilai.ToString();            
        }

        private void txtKHDua_Click(object sender, EventArgs e)
        {
            btnThanhToan.Enabled = true;
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            Ve_HoaDon vhd = new Ve_HoaDon();
            vhd.MaNV = lbMaNV.Text.Trim();
            if (txtMaKH1.Text.Trim() == "")
            {
                vhd.MaKH = 0;
            }
            else
            {
                vhd.MaKH = int.Parse(txtMaKH1.Text.Trim());
            }
            vhd.BuoiAnBuffet = cboBuoi.Text.Trim();
            vhd.Ngay = cboNgay.Text.Trim();
            vhd.VeNguoiLon = int.Parse(txtSoVeNguoiLon.Text);
            vhd.VeTreEm = int.Parse(txtSoVeTreEm.Text);
            vhd.HinhThucThanhToan = 1;
            vhd.Tong = float.Parse(lbTongTien.Text);
            vhd.GiamGia = int.Parse(cboGiamGia.Text);
            vhd.ThanhTien = float.Parse(lbPhaiTra.Text);
            vhd.NgayLap = DateTime.Now;

            vehoadon_bus = new Ve_HoaDon_BUS();
            flagThanToan = vehoadon_bus.insertThanhToanVeBuffet(vhd);
            if (flagThanToan == true)
            {
                MessageBox.Show("Thanh toán thành công!", "Thông báo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            }
            else
                MessageBox.Show("Thanh toán thất bại, vui lòng kiểm tra lại thông tin!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmKhachHang frm = new frmKhachHang();
            frm.KhiSetMaKhachHang += frm_KhiSetMaKhachHang;
            frm.Show();
        }

        void frm_KhiSetMaKhachHang(string MaKH, float diem)
        {
            txtMAKH.Text = MaKH;
            txtDiemTichLuyCon.Text = diem.ToString();
            txtMaKH1.Text = MaKH;
            txtMaKHDichVu.Text = MaKH;
            txtDiemCongDichVu.Text = diem.ToString();
        }

       
        private void txtMaKH1_TextChanged(object sender, EventArgs e)
        {
            if (txtMaKH1.Text.Trim() == "")
            {
                return;
            }
            else
            {
                if (txtDiemTichLuyCon.Text == "")
                {
                    return;
                }
                else{
                        float diemtichluycon = float.Parse(txtDiemTichLuyCon.Text.Trim());
                        DataTable phantramgiamgia = ggb.getPhanTramGiamGia(diemtichluycon);

                        int phantram = int.Parse(phantramgiamgia.Rows[0]["TiLeGiam"].ToString());
                        //cboGiamGia.SelectedItem = phantram;
                        cboGiamGia.SelectedValue = phantram;
                    }
                
            }
        }

        private void cboGiamGia_TextChanged(object sender, EventArgs e)
        {
            if (linkLabel1.Enabled == false)
            {
                lbPhaiTra.Text = lbTongTien.Text;
            }
            else
            {
                float giatri = float.Parse(lbTongTien.Text);
                float giamgia = float.Parse( cboGiamGia.Text);
                //float phaitra = giatri * (100 - giamgia);
                float phaitra = giatri - giatri * (giamgia/100);
                lbPhaiTra.Text = string.Format("{0:0}", phaitra);
            }
        }

        private void txtSoVeNguoiLon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSoVeNguoiLon.Focus();
            }
        }

        private void txtSoVeTreEm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSoVeTreEm.Focus();
            }
        }

        private void txtKHDua_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtKHDua.Focus();
            }
        }

        private void lbPhaiTra_TextChanged(object sender, EventArgs e)
        {
            float giatrihoadonphaitra = float.Parse(lbPhaiTra.Text.Trim());
            if (giatrihoadonphaitra >= 500000)
            {
                dcb = new DiemCong_BUS();
                DataTable diemcongtheohoadon = dcb.getDiemCongTheoHoadon(giatrihoadonphaitra);
                int diemcong = int.Parse(diemcongtheohoadon.Rows[0]["DiemCong"].ToString());
               //DialogResult dr =  MessageBox.Show("Khách hàng đủ điền kiện để áp dụng các chính sách ưu đãi của chúng ta!", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
               //if (DialogResult.OK == dr)
               //{
                   linkKhachHangCongDiem.Enabled = true;
                   frmKhachHang frm = new frmKhachHang();
                   frm.diemcong = diemcong;
               //}
            }
        }

        private void linkKhachHangCongDiem_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmKhachHang frm = new frmKhachHang();
            frm.KhiSetMaKhachHang += frm_KhiSetMaKHGiamGia;
            frm.Show();
        }

        void frm_KhiSetMaKHGiamGia(string MaKH, float diem)
        {
            txtMaKH1.Text = MaKH;
        }

        private bool ValidateLuhn(string value)
        {
            int CheckSum = 0;
            bool DoubleFlag = (value.Length % 2 == 0);
            char[] buf;
            char Digit;
            int DigitValue;
            // chuyển chuỗi thành ký tự
            buf = value.ToCharArray();
            // duyệt từng ký tự trong mảng
            for(int i = 0; i < value.Length; i++)
            {
                Digit = buf[i]; // đọc ký tự thứ i và gán vào biến Digit
                DigitValue = int.Parse( Digit.ToString());
                if(DoubleFlag)
                {
                    DigitValue *= 2;
                    if(DigitValue > 9)
                    {
                        DigitValue -= 9;
                    }
                }
                CheckSum += DigitValue;
                DoubleFlag = !DoubleFlag;
            }
            return (CheckSum % 10 == 0);
        }

        private void btnThanhToanATM_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Vui lòng kiểm tra hệ thống thanh toán qua ATM", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);            
        }

        private void txtMaTheATM_TextChanged(object sender, EventArgs e)
        {
            if (txtMaTheATM.Text == "")
            {
                btnThanhToanATM.Enabled = false;
            }
            else {
                if (ValidateLuhn(txtMaTheATM.Text.Trim()))
                {
                    btnThanhToanATM.Enabled = true;
                    //5191701142626689 ---> đây là một mã hợp lệ
                }
            }
            
        }

        private void btnBan2_Click(object sender, EventArgs e)
        {
            lbBan.Text = btnBan2.Text;
        }
        int soluong = 1;
        private void dgvDanhSachThucDon_DoubleClick(object sender, EventArgs e)
        {
            string tenthucdon = dgvDanhSachThucDon.CurrentRow.Cells[0].Value.ToString();
            string donvitinh = dgvDanhSachThucDon.CurrentRow.Cells[1].Value.ToString();
            string gia = dgvDanhSachThucDon.CurrentRow.Cells[2].Value.ToString();
            
            // cap nhat lai so luong
            int flag = -1;
            string Tenthucdon = dgvDanhSachThucDon.CurrentRow.Cells[0].Value.ToString();
            try
            {
                for (int i = 0; i < dgvDanhSachThucDonDaChon.Rows.Count; i++)
                {

                    if (Tenthucdon == dgvDanhSachThucDonDaChon.Rows[i].Cells[1].Value.ToString())
                        flag = i;
                }
            }
            catch(Exception ex)
            { 
            }
            if (flag != -1)
            {
                soluong = soluong + 1;
                dgvDanhSachThucDonDaChon.Rows[flag].Cells[2].Value = soluong.ToString();
                dgvDanhSachThucDonDaChon.Rows[flag].Cells[5].Value = (soluong * float.Parse(gia)).ToString();
            }
            else
            {
                soluong = 1;
                float tong = float.Parse(gia) * soluong;
                dgvDanhSachThucDonDaChon.Rows.Add(dgvDanhSachThucDonDaChon.Rows.Count.ToString(), tenthucdon, soluong, donvitinh, gia, tong.ToString());
            }


        }

        private void dgvDanhSachThucDonDaChon_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
           
            //string soluong = dgvDanhSachThucDonDaChon.CurrentRow.Cells[2].Value.ToString();
            //if (soluong != "")
            //{
            //    string gia = dgvDanhSachThucDonDaChon.CurrentRow.Cells[4].Value.ToString();
            //    float tongtien = float.Parse(soluong) * float.Parse(gia);
            //    dgvDanhSachThucDonDaChon.CurrentRow.Cells[5].Value = tongtien.ToString();
            //}
           // MessageBox.Show("s");
        }

        private void btnTongTien_Click(object sender, EventArgs e)
        {
            float tongtien = 0;
            for (int i = 0; i < dgvDanhSachThucDonDaChon.Rows.Count; i++)
            {
                string tong = dgvDanhSachThucDonDaChon.Rows[i].Cells[5].Value.ToString();
                tongtien += float.Parse(tong.Trim());
            }
            lbTongTienDichVuKhac.Text = tongtien.ToString();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int index = dgvDanhSachThucDonDaChon.CurrentRow.Index;
            dgvDanhSachThucDonDaChon.Rows.RemoveAt(index);
        }

        private void lbTongTienDichVuKhac_TextChanged(object sender, EventArgs e)
        {
            if (float.Parse(lbTongTienDichVuKhac.Text) >= 500000)
            {
                linkMaKHDichVu.Enabled = true;
            }
        }

        private void linkMaKHDichVu_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmKhachHang frm = new frmKhachHang();
            frm.KhiSetMaKhachHang += frm_KhiSetMaKhachHang;
            frm.Show();
        }

        private void txtKHDuaDichVu_TextChanged(object sender, EventArgs e)
        {
            float tongtien = float.Parse(lbTongTienDichVuKhac.Text.Trim());
            float khdua = float.Parse(txtKHDuaDichVu.Text.Trim());
            lbThoiLaiDichVu.Text = (khdua - tongtien).ToString();

        }

        private void txtKHDuaDichVu_Click(object sender, EventArgs e)
        {
            btnThanhToanDichVu.Enabled = true;
        }

        private void txtMaTheATMDichVu_TextChanged(object sender, EventArgs e)
        {
            if (txtMaTheATMDichVu.Text == "")
            {
                btnThanhToanDichVuATM.Enabled = false;
            }
            else {
                if (ValidateLuhn(txtMaTheATMDichVu.Text.Trim()))
                {
                    btnThanhToanDichVuATM.Enabled = true;
                    //5191701142626689 ---> đây là một mã hợp lệ
                }
            }
        }

        private void btnThanhToanDichVuATM_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Vui lòng kiểm tra hệ thống thanh toán qua ATM", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        }

        private void txtMaTheATM_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtMaTheATM.Focus();
            }
        }

        private void txtKHDuaDichVu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtKHDuaDichVu.Focus();
            }
        }

        private void txtMaTheATMDichVu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && (!Char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtMaTheATMDichVu.Focus();
            }
        } 
    }
}
